
"""
    登陆日志
"""

from rest_framework import serializers
from audit.models import LanDingModel


class LanDingModelSerializer(serializers.ModelSerializer):

    """
    登陆日志序列化
    """

    time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)

    class Meta:
        model = LanDingModel
        fields = ('id', 'username', 'name', 'tz', 'type', 'agent', 'ip', 'city', 'why', 'status', 'time')


class LanDingModelExportSerializer(serializers.ModelSerializer):
    """
    导出登陆日志序列化
    """
    time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)

    class Meta:
        model = LanDingModel
        fields = ('id', 'username', 'name', 'tz', 'type', 'agent', 'ip', 'city', 'why', 'status', 'time')

    def to_representation(self, instance):
        """
        修改序列化前的数据格式
        :param instance:
        :return:
        """
        data = super().to_representation(instance)

        # 状态
        if data['status'] is True:
            data['status'] = '成功'
        else:
            data['status'] = '失败'

        return data
